package com.happly.happlycloud.controller.portal;


import com.happly.happlycloud.common.ResponseCode;
import com.happly.happlycloud.common.ServerResponse;
import com.happly.happlycloud.pojo.TutorInfo;
import com.happly.happlycloud.pojo.User;
import com.happly.happlycloud.service.IMessageService;
import com.happly.happlycloud.util.CookieUtil;
import com.happly.happlycloud.util.JsonUtil;
import com.happly.happlycloud.util.RedisShardedPoolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/message/")
@Api("用户留言信息")
public class MessageController {


    @Autowired
    private IMessageService iMessageService;


    @RequestMapping("applymessage.do")
    @ApiOperation("添加")
    public ServerResponse add(HttpServletRequest httpServletRequest, TutorInfo tutorInfo){
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userJsonStr,User.class);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iMessageService.addMessage(user.getId(),tutorInfo);
    }





















}
